package com.example.jbmsuppliers.sinopec.orderaddress;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.adapter.MyListAdapter;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.util.IOrderCtrl;
import com.jbm.jbmsupplier.util.IorderEntity;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/2/28.
 */

public class MyOrderZyz extends ActivityUse {
    ArrayList<HashMap<String, Object>> arrayList;
    HashMap<String, Object> hashMap;
    @InjectView(R.id.goback_ps_set)
    Button gobackPsSet;
    @InjectView(R.id.titlelz)
    TextView titlelz;
    @InjectView(R.id.pull_refresh_list)
    PullToRefreshListView pullRefreshList;
    @InjectView(R.id.te_order_shuaxin)
    TextView teOrderShuaxin;

    private MyListAdapter myorder;
    int num = 5;
    private final int UERORDERDQR = 0001;
    DialogLoading dialogLoading;

    @Override
    protected int setContentView1() {
        return R.layout.layout_order;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        ExitAppUtlis.getInstance().addActivity(this);

        titlelz.setText("作业中");
        arrayList = new ArrayList<>();
        setPullRefreshList();
        getOrder(num);
    }


    private int count;

    public void getOrder(int num) {
        dialogLoading = new DialogLoading(this, "加载中...", true);
        NetAddRess netAddRess = new NetAddRess(handler);
        netAddRess.getZyzOrder(num, UERORDERDQR);
        if (dialogLoading != null)
            dialogLoading.show();


    }

    private void setPullRefreshList() {

        pullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getOrder(num);
                        myorder.notifyDataSetChanged();
                        pullRefreshList.onRefreshComplete();
                    }
                }, 1000);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getOrder(num += 5);
                if (myorder != null)
                    myorder.notifyDataSetChanged();
                pullRefreshList.onRefreshComplete();
            }
        });
        pullRefreshList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MyOrderZyz.this, MyOrderDetail.class);
                intent.putExtra("order_id", arrayList.get(i).get("order_id") + "");
                intent.putExtra("status", arrayList.get(i).get("status") + "");
                startActivity(intent);

            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UERORDERDQR: {
                    final IOrderCtrl result = (IOrderCtrl) msg.obj;
                    if (result.back) {
                        if (dialogLoading != null)
                            dialogLoading.dismiss();
                        if (result.orderList.size()>0) {
                            arrayList.clear();
                            for (int i = 0; i < result.orderList.size(); i++) {
                                hashMap = new HashMap<>();
                                IorderEntity or = result.orderList.get(i);
                                hashMap.put("order_id",or .getOrderId());//id
                                hashMap.put("status",or.getStatus());//状态
                                hashMap.put("amount", or.getAmount());//价格
                                hashMap.put("goods_code", or.getAmount());//维修价格
                                hashMap.put("item_desc", or.getContacts());//姓名
                                hashMap.put("create_date",or.getMobile());//手机号
                                hashMap.put("name", or.getGiText());//地址
                                arrayList.add(hashMap);
                            }
                            if (result.orderList.size()<6){
                                myorder = new MyListAdapter(MyOrderZyz.this, arrayList, "all");
                                pullRefreshList.setAdapter(myorder);}else{
                                myorder. setNotify(arrayList);

                            }
                        } else {
                            teOrderShuaxin.setVisibility(View.VISIBLE);
                            pullRefreshList.setVisibility(View.GONE);
                            return;
                        }
                    } else if (result.equals("time out")) {
                        if (dialogLoading != null)
                            dialogLoading.dismiss();
                        hint(result.errorinfo);
                    } else {
                        if (dialogLoading != null)
                            dialogLoading.dismiss();
                        hint(result.errorinfo);
                    }
                    break;
                }
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        if (myorder != null)
            myorder.notifyDataSetChanged();
    }

    @OnClick(R.id.goback_ps_set)
    public void onClick() {
        finish();
    }
}
